package cn.stylefeng.guns.modular.system.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.order.model.params.OutOrderParam;
import cn.stylefeng.guns.modular.order.service.OutOrderService;
import cn.stylefeng.guns.modular.storage.model.params.StorageOutDetailedParam;
import cn.stylefeng.guns.modular.storage.model.params.StoragePutSingleParam;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageOutDetailedResult;
import cn.stylefeng.guns.modular.storage.model.result.StoragePutSingleResult;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockResult;
import cn.stylefeng.guns.modular.storage.service.StorageOutDetailedService;
import cn.stylefeng.guns.modular.storage.service.StoragePutSingleService;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 查询 TMS面板 数据
 */
@Controller
@RequestMapping("/panelWms")
public class panelWmsController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private StorageStockService storageStockService;

    @Autowired
    private StoragePutSingleService storagePutSingleService;

    @Autowired
    private StorageOutDetailedService storageOutDetailedService;
    
    /**
     *查询可选择仓库
     */
    @ResponseBody
    @RequestMapping("/findWarehouse")
    public ResponseData findWarehouse() {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
        return ResponseData.success(basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
    }
    
    /**
     *查询 货物总量
     */
    @ResponseBody
    @RequestMapping("/sumGoodsZongliang")
    public ResponseData sumGoodsZongliang(StorageStockParam storageStockParam) {
        return ResponseData.success(storageStockService.sumGoodsZongliang(storageStockParam));
    }
    
    /**
     *查询 货物总量构成
     */
    @ResponseBody
    @RequestMapping("/sumGoods")
    public ResponseData sumGoods(StorageStockParam storageStockParam) {
        return ResponseData.success(storageStockService.sumGoods(storageStockParam));
    }
    
    /**
     *查询 月出入库统计
     */
    @ResponseBody
    @RequestMapping("/monthPutOutCount")
    public ResponseData monthPutOutCount(StorageStockParam storageStockParam) {
    	List<StorageStockResult> listres = new ArrayList<StorageStockResult>();
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        for (int i=0;i>-5;i--){//获取最近5个月
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,i);
            dateList.add(simpleDateFormat.format(calendar.getTime()));
        }
        Collections.sort(dateList);
        for (String month : dateList) {
        	StorageStockResult ss = new StorageStockResult();
        	ss.setMark(month);
        	
        	StoragePutSingleParam storagePutSingleParam = new StoragePutSingleParam();
        	storagePutSingleParam.setStorageNumber(month);
        	StoragePutSingleResult storagePutSingleResult = storagePutSingleService.monthPutCount(storagePutSingleParam,storageStockParam.getWarehouseId());//查询月   入库数
        	if(storagePutSingleResult != null) {
        		if(ToolUtil.isNotEmpty(storagePutSingleResult.getPutNum())) {
                	ss.setNum(storagePutSingleResult.getPutNum());
        		} else {
                	ss.setNum("0");
        		}
        	} else {
            	ss.setNum("0");
    		}
        	
        	StorageOutDetailedParam storageOutDetailedParam = new StorageOutDetailedParam();
        	storageOutDetailedParam.setStorageOutNumber(month);
        	StorageOutDetailedResult storageOutDetailedResult = storageOutDetailedService.monthOutCount(storageOutDetailedParam,storageStockParam.getWarehouseId());//查询月  出库数
        	if(storageOutDetailedResult != null) {
        		if(ToolUtil.isNotEmpty(storageOutDetailedResult.getRealNum())) {
                	ss.setBalenum(storageOutDetailedResult.getRealNum());
        		} else {
                	ss.setBalenum("0");
        		}
        	} else {
            	ss.setBalenum("0");
    		}
        	listres.add(ss);
		}
        return ResponseData.success(listres);
    }
    
    /**
     *查询 出库量货品月排名
     */
    @ResponseBody
    @RequestMapping("/monthOutGoodsCount")
    public ResponseData monthOutGoodsCount(StorageStockParam storageStockParam,String month) {
    	StorageOutDetailedParam storageOutDetailedParam = new StorageOutDetailedParam();
    	storageOutDetailedParam.setStorageOutNumber(month);
    	List<StorageOutDetailedResult> list = storageOutDetailedService.monthOutGoodsCount(storageOutDetailedParam,storageStockParam.getWarehouseId());//查询月  月出库量货品排名
        return ResponseData.success(list);
    }
    
    /**
     *查询 手机端拣选作业月排名
     */
    @ResponseBody
    @RequestMapping("/appMonthJianxuanCount")
    public ResponseData appMonthJianxuanCount(StorageStockParam storageStockParam,String month) {
    	StorageOutDetailedParam storageOutDetailedParam = new StorageOutDetailedParam();
    	storageOutDetailedParam.setStorageOutNumber(month);
    	List<StorageOutDetailedResult> list = storageOutDetailedService.appMonthJianxuanCount(storageOutDetailedParam,storageStockParam.getWarehouseId());//查询月 手机端拣选作业月排名
        return ResponseData.success(list);
    }

    @Autowired
    private OutOrderService outOrderService;
    
    /**
     *查询 出库订单分析
     */
    @ResponseBody
    @RequestMapping("/outOrderCount")
    public ResponseData outOrderCount(StorageStockParam storageStockParam) {
    	OutOrderParam outOrderParam = new OutOrderParam();
    	outOrderParam.setOutOrderNumber(DateUtil.getDate());
    	Integer num1 = outOrderService.outOrderCount(outOrderParam,storageStockParam.getWarehouseId());//查询 今日已完成订单数
    	Integer num2 = outOrderService.outNewOrderCount(outOrderParam,storageStockParam.getWarehouseId());//查询 今日新订单数
    	Integer num3 = outOrderService.outWeiwanchengOrderCount(outOrderParam,storageStockParam.getWarehouseId());//查询 未完成订单数
    	Integer num4 = outOrderService.outYuqiOrderCount(outOrderParam,storageStockParam.getWarehouseId());//查询 逾期订单数
    	Integer num5 = outOrderService.outZuofeiOrderCount(outOrderParam,storageStockParam.getWarehouseId());//查询 已取消订单数
    	outOrderParam.setOutOrderStatus(num1.toString());
    	outOrderParam.setOutOrderNumber(num2.toString());
    	outOrderParam.setOutCustomerName(num3.toString());
    	outOrderParam.setCustomerNumber(num4.toString());
    	outOrderParam.setSaleDeliveryCompany(num5.toString());
        return ResponseData.success(outOrderParam);
    }
    
    /**
     *查询 仓储流动数据
     */
    @ResponseBody
    @RequestMapping("/wmsData")
    public ResponseData wmsData(StorageStockParam storageStockParam) {
    	StorageOutDetailedParam storageOutDetailedParam = new StorageOutDetailedParam();
    	List<StorageOutDetailedResult> list = storageOutDetailedService.wmsData(storageOutDetailedParam,storageStockParam.getWarehouseId());//查询最新5条出库记录
        return ResponseData.success(list);
    }
    
    /**
     *查询 质保期汇总分析
     */
    @ResponseBody
    @RequestMapping("/baozhiqiCount")
    public ResponseData baozhiqiCount(StorageStockParam storageStockParam) {
    	storageStockParam.setPutTime(DateUtil.getDate());
    	Integer num1 = storageStockService.baozhiqiCount(storageStockParam);//查询保质期已过3/1存货量
    	Integer num2 = storageStockService.baozhiqi2Count(storageStockParam);//查询保质期已过2/1存货量
    	Integer num3 = storageStockService.baozhiqi1Count(storageStockParam);//查询保质期剩余1个月以内存货量
    	Integer num4 = storageStockService.baozhiqiGuoqiCount(storageStockParam);//查询保质期已过期存货量
    	storageStockParam.setMark(ToolUtil.isNum(num1)?num1.toString():"0");
    	storageStockParam.setOperateName(ToolUtil.isNum(num2)?num2.toString():"0");
    	storageStockParam.setCustomerName(ToolUtil.isNum(num3)?num3.toString():"0");
    	storageStockParam.setWarehouseName(ToolUtil.isNum(num4)?num4.toString():"0");
        return ResponseData.success(storageStockParam);
    }

}
